package com.zhongfl.crm.lixiao.config.task;

import com.zhongfl.crm.lixiao.batch.jobs.*;
import com.zhongfl.crm.lixiao.batch.task.OrderRevisitLogSyncFromBoss2LXTask;
import com.zhongfl.crm.lixiao.batch.task.OrderSyncFromBoss2LXTask;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import tech.heptagram.core.common.batch.BatchExecutor;

import javax.annotation.Resource;

/**
 * 任务配置
 *
 * @author 木叶丸
 */
@Configuration
public class LxTaskConfig {

    private final static long ONE_MINUTE = 60 * 1000;
    private final static long TEN_MINUTE = 10 * ONE_MINUTE;

    @Value("${task.default.run}")
    private boolean defaultRunStatus;

    @Resource
    private LxDepartmentSyncJob lxDepartmentSyncJob;

    @Resource
    private LxUserSyncJob lxUserSyncJob;

    @Resource
    private LxChannelSyncJob lxChannelSyncJob;

    @Resource
    private OrderSyncFromLX2BossJob orderSyncFromLX2BossJob;

    @Resource
    private LXChannelRevisitLog2BossSyncJob lxChannelRevisitLog2BossSyncJob;

    @Resource
    private OrderRevisitLogSyncFromLX2BossJob orderRevisitLogSyncFromLX2BossJob;

    @Resource
    private OrderSyncFromBoss2LXTask orderSyncFromBoss2LXTask;

    @Resource
    private OrderRevisitLogSyncFromBoss2LXTask orderRevisitLogSyncFromBoss2LXTask;

    /**
     * 任务池初始化
     */
    @Bean
    @ConditionalOnMissingBean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        return executor;
    }

    /**
     * 是否执行任务配置 --> 配置文件中
     */
    private BatchExecutor getDefaultBatchExecutor() {
        BatchExecutor executor = new BatchExecutor();
        executor.setTaskThreadPool(taskExecutor());
        executor.setStart(defaultRunStatus);
        return executor;
    }

    /**
     *
     */
    @Bean
    public BatchExecutor lxDepartmentSyncJobExecutor() {
        BatchExecutor defaultBatchExecutor = getDefaultBatchExecutor();
        defaultBatchExecutor.setBatchService(lxDepartmentSyncJob);
        return defaultBatchExecutor;
    }

    @Bean
    public BatchExecutor lxUserSyncJobExecutor() {
        BatchExecutor defaultBatchExecutor = getDefaultBatchExecutor();
        defaultBatchExecutor.setBatchService(lxUserSyncJob);
        return defaultBatchExecutor;
    }

    @Bean
    public BatchExecutor lxChannelSyncJobExecutor() {
        BatchExecutor defaultBatchExecutor = getDefaultBatchExecutor();
        defaultBatchExecutor.setBatchService(lxChannelSyncJob);
        return defaultBatchExecutor;
    }

    @Bean
    public BatchExecutor orderSyncFromLX2BossJobExecutor() {
        BatchExecutor defaultBatchExecutor = getDefaultBatchExecutor();
        defaultBatchExecutor.setBatchService(orderSyncFromLX2BossJob);
        return defaultBatchExecutor;
    }

    @Bean
    public BatchExecutor orderSyncFromBoss2LXTaskExecutor() {
        BatchExecutor executor = getDefaultBatchExecutor();
        executor.setBatchService(orderSyncFromBoss2LXTask);
        return executor;
    }

    @Bean
    public BatchExecutor orderRevisitLogSyncFromBoss2LXTaskExecutor() {
        BatchExecutor executor = getDefaultBatchExecutor();
        executor.setBatchService(orderRevisitLogSyncFromBoss2LXTask);
        return executor;
    }

    @Bean
    public BatchExecutor orderRevisitLogSyncFromLX2BossJobExecutor() {
        BatchExecutor defaultBatchExecutor = getDefaultBatchExecutor();
        defaultBatchExecutor.setBatchService(orderRevisitLogSyncFromLX2BossJob);
        return defaultBatchExecutor;
    }

    @Bean
    public BatchExecutor lxChannelRevisitLog2BossSyncJobExecutor() {
        BatchExecutor executor = getDefaultBatchExecutor();
        executor.setBatchService(lxChannelRevisitLog2BossSyncJob);
        return executor;
    }
}
